<script setup lang="ts">
/**
 * @see {@link https://vuejs.org/api/sfc-script-setup.html#defineoptions}
 */
import {
  withdrawalList,
  handleApply,
  withdrawalApplyRead
} from "@/api/order";

import { withdrawalQuery, withdList } from "@/api/order/types";
import { formatTimestamp } from '@/utils/index'

defineOptions({
  name: "User",
  inheritAttrs: false,
});

const queryFormRef = ref(ElForm); // 查询表单

const loading = ref(false);
const ids = ref([]);
const total = ref(0);
const dialog = reactive<DialogOption>({
  visible: false,
});

const queryParams = reactive<withdrawalQuery>({
  page: 1,
  ps: 10,
});

const dataList = ref<withdList[]>();

/**
 * 查询
 */
function handleQuery() {
  loading.value = true;
  withdrawalList(queryParams)
    .then(({ data }) => {
      dataList.value = data.data;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.page = 1;
  handleQuery();
}

const handReadMessages = (id: number) => {
  withdrawalApplyRead({ id: id }).then((res) => {
    ElMessage({
      type: 'success',
      message: '操作成功',
    })
  }).finally(() => {
    handleQuery();
  })
}

// 确认提现
const hanldeOperation = (id: number, type: number | string) => {
  ElMessageBox.confirm('确认此操作吗?', '确认此操作吗', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {

    // 提交
    handleApply({ id, apply_status: type }).then((res: any) => {
      console.log(res);
      ElMessage({
        type: 'success',
        message: res.msg,
      })
      resetQuery()
    })

  }).catch(() => {
    ElMessage({
      type: 'info',
      message: 'Delete canceled',
    })
  })
}

onMounted(() => {
  handleQuery(); // 初始化用户列表数据
});
</script>

<template>
  <div class="app-container">
    <el-row :gutter="20">

      <el-col :lg="24" :xs="24">
        <div class="search-container">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="100px">
            <el-form-item label="上级账号" prop="apply_status">
              <el-input v-model="queryParams.apply_status" placeholder="输入素材名称" clearable style="width: 200px"
                @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery"><i-ep-search />搜索</el-button>
              <el-button @click="resetQuery">
                <i-ep-refresh />
                重置</el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-card shadow="never">
          <el-table v-loading="loading" :data="dataList">
            <el-table-column label="申请id" align="center" prop="id" width="100" />
            <el-table-column label="申请金额" align="center" prop="amount" />
            <el-table-column label="打款状态" align="center">
              <template #default="scope">
                <el-tag type="success" v-if="scope.row.apply_status == 1">未处理</el-tag>
                <el-tag type="success" v-if="scope.row.apply_status == 3">已打款</el-tag>
                <el-tag type="success" v-if="scope.row.apply_status == 5">撤销</el-tag>
              </template>
            </el-table-column>
            <el-table-column width="180" label="申请时间" align="center" prop="created_at">
              <template #default="scope">
                {{ formatTimestamp(scope.row.created_at * 1000) }}
              </template>
            </el-table-column>
            <el-table-column label="提现链路" align="center" prop="chain_type" />
            <el-table-column label="持有人" align="center" prop="username" />
            <el-table-column label="钱包地址" align="center" prop="wallet_address" />
            <el-table-column label="提现手续" align="center" prop="charge" />
            <el-table-column label="实际提现金额" align="center" prop="act_income" />
            <el-table-column label="状态" align="center">
              <template #default="scope">
                <el-tag v-if="scope.row.is_read === 1 || scope.row.is_read === 0" type="danger">未读</el-tag>
                <el-tag v-if="scope.row.is_read === 2" type="primary">已读</el-tag>
              </template>
            </el-table-column>
            <el-table-column fixed="right" align="center" label="操作" width="220">
              <template #default="scope">
                <div v-if="scope.row.apply_status == 1">
                  <el-button type="primary" link size="small" @click.stop="hanldeOperation(scope.row.id, 3)">
                    确认打款
                  </el-button>
                  <el-button type="danger" link size="small" @click.stop="hanldeOperation(scope.row.id, 5)">
                    撤销
                  </el-button>
                  <el-button v-if="scope.row.is_read === 1 || scope.row.is_read === 0" type="primary" link size="small"
                    @click.stop="handReadMessages(scope.row.id)">已读</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.page"
            v-model:limit="queryParams.ps" @pagination="handleQuery" />
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
